package com.example.d3.character.compare;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
 
public class Character extends ListActivity{
	private HeroData hero;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.character_stats);
 
        TextView tvName = (TextView) findViewById(R.id.name);
        
        Intent i = getIntent();
        // getting attached intent data
        String url = i.getStringExtra("URL");
        int heroNum = i.getIntExtra("HeroNum", 1);

        D3CompareApp app = (D3CompareApp)getApplication();
        if(heroNum == 2){
        	hero = app.getHero2();
        } else {
        	hero = app.getHero1();
        }
 
        if(!hero.isValid()){
        	//error
        	return;
        }
        String strChar = app.getHero1().name();
        HeroData secHero = app.getHero2();
        if(secHero.isValid()){
        	strChar += " Compared to: " + secHero.name();
        }
        int heroID = hero.id();
        
        // displaying selected hero
        tvName.setText("Select a stat to compare. Current hero: " + strChar);

        AQuery aq = new AQuery(this);
    	url += "hero/" + Integer.toString(heroID); //http://us.battle.net/api/d3/profile/banished-1684/hero/5331557
        aq.ajax(url, JSONObject.class, this, "jsonCallback");
    }
    
    public void onNewCharacter(View v){
    	//load character selection view for current account
    	onBackPressed();
    }
    
    public void onNewAccount(View v){
    	//load login screen for new account selection
    	Intent intent = new Intent(this, AccountSelectionActivity.class);
    	startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }
        
    public void jsonCallback(String url, JSONObject heroJSON, AjaxStatus status){    	
    	if(heroJSON == null){
    		//failed to load character data
    		TextView textView = new TextView(this);
            textView.setTextSize(40);
            textView.setText("Error: Unable to locate hero");
        	setContentView(textView);
    		return;
    	}
		try {			
			JSONObject stats = heroJSON.getJSONObject("stats");
			JSONArray names = stats.names();
			for(int i = 0; i < names.length(); i++){
				String name = names.getString(i);
				hero.addStat(name,stats.getDouble(name));
			}
		} catch (JSONException e) {
			TextView textView = new TextView(this);
            textView.setTextSize(40);
			textView.setText(e.toString());
			setContentView(textView);
			e.printStackTrace();
		}

		D3CompareApp app = (D3CompareApp)getApplication();
		this.setListAdapter(new StatAdapter(this, app.getHero1(), app.getHero2()));
    }
    
}